package top.wshape1.easyscan.ui;

import android.os.Bundle;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;

import java.nio.file.attribute.AttributeView;
import java.util.concurrent.atomic.AtomicInteger;

import top.wshape1.easyscan.R;
import top.wshape1.easyscan.activity.EasyScanActivity;
import top.wshape1.easyscan.custom.CustomRuleListDialog;
import top.wshape1.easyscan.data.SettingData;
import top.wshape1.easyscan.databinding.FragmentSettingBinding;
import top.wshape1.easyscan.entity.CustomRule;

public class SettingFragment extends Fragment {
    public static final String TAG = "SettingFragment";
    private FragmentSettingBinding binding;

    private EasyScanActivity mainActivity;

    /**
     * 配置操作类
     */
    private SettingData settingData;
    /**
     * 将 导航fragment 对应的id放在数组内
     */
    private int[] fragmentNavIds;
    /**
     * 将 导航fragment 对应的名称放在数组内
     */
    private String[] fragmentStrings;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mainActivity = (EasyScanActivity) getActivity();
    }

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {

        binding = FragmentSettingBinding.inflate(inflater, container, false);
        View root = binding.getRoot();

        // 获取数据操作类
        settingData = mainActivity.settingData;


        // fragment数据初始化
        fragmentNavIds = new int[]{
                R.id.navigation_home,
                R.id.navigation_scan,
                R.id.navigation_setting};

        fragmentStrings = new String[]{
                mainActivity.getString(R.string.title_home),
                mainActivity.getString(R.string.title_scan),
                mainActivity.getString(R.string.title_setting)};


        // 设置文本的初始化
        binding.settingSelectedFragment.setText(fragmentStrings[getIndexByNavId(settingData.getDefaultFragment())]);
        binding.settingUrlSwitch.setChecked(settingData.isUrlJump());

        // 设置事件
        binding.blockDefaultFragment.setOnClickListener(blockDefaultFragmentClickListener);
        binding.settingUrlSwitch.setOnCheckedChangeListener(urlJumpSwitchCheckedChangeListener);
        binding.blockCustomRule.setOnClickListener(blockCustomRuleClickListener);

        return root;
    }

    /**
     * 通过导航栏资源id获取在数组fragmentNavIds中对应的索引
     *
     * @param id 资源ID
     * @return index of fragmentNavIds[]
     */
    private int getIndexByNavId(int id) {
        for (int i = 0; i < fragmentNavIds.length; i++) {
            if (fragmentNavIds[i] == id) {
                return i;
            }
        }
        return 0;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }


    /**
     * 默认页面 切换事件
     */
    private final View.OnClickListener blockDefaultFragmentClickListener = v -> {

        int setFragment = settingData.getDefaultFragment();
        AtomicInteger selectedItemIndex = new AtomicInteger();
        selectedItemIndex.set(getIndexByNavId(setFragment));

        AlertDialog.Builder builder = new AlertDialog.Builder(mainActivity);
        builder.setTitle(R.string.block_default_fragment_title)
                .setSingleChoiceItems(fragmentStrings, selectedItemIndex.get(), (dialog, which) -> selectedItemIndex.set(which))
                .setNegativeButton("取消", (dialog, which) -> dialog.dismiss())
                .setPositiveButton("确定", (dialog, which) -> {
                    settingData.setDefaultFragment(fragmentNavIds[selectedItemIndex.get()]);
                    binding.settingSelectedFragment.setText(fragmentStrings[selectedItemIndex.get()]);
                })
                .create()
                .show();
    };

    /**
     * 是否url跳转 按钮切换事件
     */
    private final CompoundButton.OnCheckedChangeListener urlJumpSwitchCheckedChangeListener = (buttonView, isChecked) -> settingData.setUrlJump(isChecked);

    /**
     * 自定义规则 点击事件
     */
    private View.OnClickListener blockCustomRuleClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            CustomRuleListDialog customRuleListDialog = new CustomRuleListDialog(mainActivity);


            customRuleListDialog.show();

        }
    };
}